package com.bonait.hotapp;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.multidex.MultiDex;

import com.tencent.tinker.anno.DefaultLifeCycle;
import com.tencent.tinker.entry.DefaultApplicationLike;
import com.tencent.tinker.lib.listener.DefaultPatchListener;
import com.tencent.tinker.lib.patch.AbstractPatch;
import com.tencent.tinker.lib.patch.UpgradePatch;
import com.tencent.tinker.lib.reporter.DefaultLoadReporter;
import com.tencent.tinker.lib.reporter.DefaultPatchReporter;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareConstants;

@DefaultLifeCycle(
        application = "com.bonait.hotapp.MyApplication",             //application name to generate
        flags = ShareConstants.TINKER_ENABLE_ALL)
public class MyApplicationLike extends DefaultApplicationLike {

    public MyApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(base);
        AbstractPatch upgradePatchProcessor = new UpgradePatch();
        TinkerInstaller.install(this
                , new DefaultLoadReporter(getApplication())
                , new DefaultPatchReporter(getApplication())
                , new DefaultPatchListener(getApplication())
                , SampleResultService.class
                , upgradePatchProcessor);
        Tinker tinker = Tinker.with(getApplication());
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        getApplication().registerActivityLifecycleCallbacks(callback);
    }


    //自己测试true打印日志(测试的时候 true 正式包时候 false)
    private boolean isDebug = true;

    @Override
    public void onCreate() {
        super.onCreate();

        //-----------------------------------------------------------------------------------------------------
        //LogUtils.customTagPrefix = "itchen";
        //LogUtils.allowD = isDebug;//

        com.blankj.utilcode.util.Utils.init(this.getApplication());
        initLog();

        //-----------------------------------------------------------------------------------------------------

//        KalleConfig config = KalleConfig.newBuilder()
//                //.setHeader("Accept-Encoding", "identity")
////                .setHeader("Connection", "close")//2021-7-17 10:33:18 为了测试unexpected end of stream问题而修改。
//                //Connection
//                //.addHeader(ConstantApi.USERTOKEN, session.getToken())//userToken  假如首次安装是null
//                //.addHeader(ConstantApi.DEVICECODE, session.getUuid())//deviceCode 假如首次安装是nul
//                .connectFactory(OkHttpConnectFactory.newBuilder().build())
//                .cookieStore(DBCookieStore.newBuilder(this.getApplication()).build())
//                //.cacheStore(DiskCacheStore.newBuilder(AppConfig.get().PATH_APP_CACHE).build())
//                .network(new BroadcastNetwork(this.getApplication()))//这个建议添加上
//                //.addInterceptor(new RetryInterceptor(1))//重试 1 次数的拦截器,2021-7-17 10:11:33
//                .connectionTimeout(60, TimeUnit.SECONDS)//网络连接时间默认60秒
//                .readTimeout(60, TimeUnit.SECONDS)//
//                //.workThreadExecutor(Executor)
//                //.addInterceptor(new LoggerInterceptor ("itchen--KalleSample", isDebug))
//                //.converter(new JsonConverter(this))//配置全局的转换器
//                .build();

        //interceptor.setLevel(HttpLoggingInterceptor.Level.NONE);

        //而且再gettoken之中也要加前缀 communitySteward_
        //形式如：GET http://wuyeapi.91dgj.com//pub/v1/token/getToken/communitySteward_4385c2b7089fbcd1 HTTP/1.1
        //本来原始的User-Agent是User-Agent: Mozilla/5.0 (Linux; U; Android 9; en; DUK-AL20 Api/HUAWEIDUK-AL20) AppleWebKit/534.30 (KHTML, like Gecko) Version/5.0 Mobile Safari/534.30
        //User-Agent: CommunitySteward/Mozilla/5.0 (Linux; U; Android 9; zh-cn; DUK-AL20 Build/HUAWEIDUK-AL20) AppleWebKit/533.1 (KHTML, like Gecko) Version/5.0 Mobile Safari/533.1
        //原始的：User-Agent
        //List<String> userAgentOriginalList = config.getHeaders().get("User-Agent");
        //config.getHeaders().remove("User-Agent");//得把早期原始的清除掉

        //LogUtils.d("itchen--->userAgentOriginalList="+userAgentOriginalList);
        //itchen--->userAgentOriginal=[Mozilla/5.0 (Linux; U; Android 9; en; DUK-AL20 Api/HUAWEIDUK-AL20) AppleWebKit/534.30 (KHTML, like Gecko) Version/5.0 Mobile Safari/534.30]

        //userAgentOriginal.add(0,"17531108969/");
        //LogUtils.d("itchen--->userAgentLast="+userAgentOriginal);
        //itchen--->userAgentLast=[17531108969/, Mozilla/5.0 (Linux; U; Android 9; en; DUK-AL20 Api/HUAWEIDUK-AL20) AppleWebKit/534.30 (KHTML, like Gecko) Version/5.0 Mobile Safari/534.30]

//        if (userAgentOriginalList != null && !userAgentOriginalList.isEmpty()) {
//            String temp = "CloudSteward/" + userAgentOriginalList.get(0);
//            userAgentOriginalList.set(0, temp);
//        }

        //LogUtils.d("itchen--->userAgentLast="+userAgentOriginalList);
        //itchen--->userAgentLast=[17531108969/Mozilla/5.0 (Linux; U; Android 9; en; DUK-AL20 Api/HUAWEIDUK-AL20) AppleWebKit/534.30 (KHTML, like Gecko) Version/5.0 Mobile Safari/534.30]

        //config.getHeaders().add("User-Agent", userAgentOriginalList);

        //config.getHeaders().remove("Connection");
        //config.getHeaders().add("Connection", "close");
        //config.getHeaders().set("Accept-Encoding", "identity");

//        Kalle.setConfig(config);

        //---------------------------------------------

        //-----------------------------------------------------------------------------------------------------


    }

    private void initLog() {

        //final com.blankj.utilcode.util.LogUtils.Config config = com.blankj.utilcode.util.LogUtils.getConfig()
        com.blankj.utilcode.util.LogUtils.getConfig()
                .setLogSwitch(isDebug)// 设置 log 总开关，包括输出到控制台和文件，默认开
                .setConsoleSwitch(isDebug)// 设置是否输出到控制台开关，默认开
                .setGlobalTag("itchen")// 设置 log 全局标签，默认为空
                // 当全局标签不为空时，我们输出的 log 全部为该 tag，
                // 为空时，如果传入的 tag 为空那就显示类名，否则显示 tag
                .setLogHeadSwitch(isDebug)// 设置 log 头信息开关，默认为开
                .setLog2FileSwitch(true)// 打印 log 时是否存到文件的开关，默认关
                .setSingleTagSwitch(true)// 一条日志仅输出一条，默认开，为美化 AS 3.1 的 Logcat
                .setDir("")// 当自定义路径为空时，写入应用的/cache/log/目录中
                .setFilePrefix("itchen")// 当文件前缀为空时，默认为"util"，即写入文件为"util-MM-dd.txt"
                .setBorderSwitch(true)// 输出日志是否带边框开关，默认开
                .setConsoleFilter(com.blankj.utilcode.util.LogUtils.V)// log 的控制台过滤器，和 logcat 过滤器同理，默认 Verbose
                .setFileFilter(com.blankj.utilcode.util.LogUtils.E)// log 文件过滤器，和 logcat 过滤器同理，默认 Verbose
                .setStackDeep(1)// log 栈深度，默认为 1
                .setSaveDays(3);//  设置日志可保留天数，默认为 -1 表示无限时长


    }

}
